package com.come4loves.game.dbproxy.entity;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.come4loves.core.dbcenter.DBQueueType;
import com.come4loves.core.dbcenter.Entity;
import com.come4loves.core.dbcenter.annotation.Column;
import com.come4loves.core.dbcenter.annotation.Table;
import com.come4loves.core.utils.Splitable;
import com.come4loves.core.utils.StringUtils;
import com.come4loves.game.dataconfig.service.EquipCardService;
import com.come4loves.game.dataconfig.service.GeneralCardService;
import com.come4loves.game.dataconfig.service.SpellCardService;
import com.come4loves.game.dataconfig.service.UnitCardService;
import com.come4loves.game.module.card.model.CardModel;

@Table(name = "card", type = DBQueueType.IMPORTANT)
public class Card extends Entity<Long> {

	private static final long serialVersionUID = -7847248383473113914L;
	
	@Column(pk = true)
	private long actorId;
	
	
	@Column
	private String generalCard;
	
	@Column
	private String equipCard;
	
	@Column
	private String unitCard;
	
	@Column
	private String spellCard;

	
	public Map<Integer, CardModel> generCardsMap = new HashMap<Integer, CardModel>();
	
	public Map<Integer, CardModel> equipCardsMap = new HashMap<Integer, CardModel>();
	
	public Map<Integer, CardModel> unitCardsMap = new HashMap<Integer, CardModel>();
	
	public Map<Integer, CardModel> spellCardsMap = new HashMap<Integer, CardModel>();
	
	public void initCard4Fresh() {
		this.generalCard = GeneralCardService.exportAllGeneral();
		this.equipCard = EquipCardService.exportAllEquip();
		this.unitCard = UnitCardService.exportAllUnit();
		this.spellCard = SpellCardService.exportAllSpell();
		this.hasReadEvent();
	}
	
	@Override
	public Long getPkId() {
		return actorId;
	}

	@Override
	public void setPkId(Long pk) {
		this.actorId = pk;

	}

	@Override
	protected Entity<Long> readData(ResultSet rs, int rowNum) throws SQLException {
		Card card = new Card();
		card.actorId = rs.getLong("actorId");
		card.generalCard = rs.getString("generalCard");
		card.equipCard = rs.getString("equipCard");
		card.unitCard = rs.getString("unitCard");
		card.spellCard = rs.getString("spellCard");
		return card;
	}

	@Override
	protected void hasReadEvent() {
		List<CardModel> generalList = StringUtils.delimiterString2List(generalCard, CardModel.class, Splitable.ELEMENT_SPLIT);
		for (CardModel cardModel : generalList) {
			generCardsMap.put(cardModel.cfgId, cardModel);
		}
		List<CardModel> equipList = StringUtils.delimiterString2List(equipCard, CardModel.class, Splitable.ELEMENT_SPLIT);
		for (CardModel cardModel : equipList) {
			equipCardsMap.put(cardModel.cfgId, cardModel);
		}
		List<CardModel> unitList = StringUtils.delimiterString2List(unitCard, CardModel.class, Splitable.ELEMENT_SPLIT);
		for (CardModel cardModel : unitList) {
			unitCardsMap.put(cardModel.cfgId, cardModel);
		}
		List<CardModel> spellList = StringUtils.delimiterString2List(spellCard, CardModel.class, Splitable.ELEMENT_SPLIT);
		for (CardModel cardModel : spellList) {
			spellCardsMap.put(cardModel.cfgId, cardModel);
		}
	}

	@Override
	protected ArrayList<Object> writeData(Boolean containsPK) {
		ArrayList<Object> values = new ArrayList<Object>();
		if (containsPK) {
			values.add(this.actorId);
		}
		values.add(this.generalCard);
		values.add(this.equipCard);
		values.add(this.unitCard);
		values.add(this.spellCard);
		return values;
	}

	@Override
	protected void beforeWritingEvent() {
		this.generalCard = StringUtils.delimiterTCollection2String(this.generCardsMap.values());
		this.equipCard = StringUtils.delimiterTCollection2String(this.equipCardsMap.values());
		this.unitCard = StringUtils.delimiterTCollection2String(this.unitCardsMap.values());
		this.spellCard = StringUtils.delimiterTCollection2String(this.spellCardsMap.values());
	}

	@Override
	protected void disposeBlob() {
		generalCard = EMPTY_STRING;
		equipCard = EMPTY_STRING;
		unitCard = EMPTY_STRING;
		spellCard = EMPTY_STRING;
	}

}
